package com.ets.controller;

import javax.annotation.Resource;
import javax.servlet.http.*;
import java.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ets.entity.*;
import com.ets.service.*;
@Controller
@RequestMapping("manage")
public class ManagerController {
	@Resource
	private ManagerService mService;
	@Resource
	private StudentService sService;
	@Resource
	private StuResultService srService;
	@Resource
	private LessonService lService;
	@Resource
	private PaperService pService;
	@Resource
	private QuestionService qService;
	//跳转至管理员登录界面
	@RequestMapping("goLogin")
	public String goLogin(){
		return "manage/login";
	}
	
	//验证登录信息
	@RequestMapping("login")
	public String login(HttpServletRequest req,ModelMap model){
		String name=req.getParameter("name");
		String pwd=req.getParameter("pwd");
		Manager manager=mService.checkLogin(name, pwd);

		if(manager==null){
			String info="用户名或密码错误！";
			String url="goLogin.do";
			model.put("info", info);
			model.put("url", url);
			return "manage/result";
		}					
		req.getSession().setAttribute("loginManager", manager);
		model.put("page", "welcome.jsp");
		return "manage/main";
	}
	
	//managers info
	@RequestMapping("manager")
	public String manager(ModelMap model){
		List<Manager> managers=mService.getAllManagers();
		model.put("managers", managers);
		model.put("page", "manager.jsp");
		return "manage/main";
	}
	
	//显示学生信息
	@RequestMapping("student")
	public String student(ModelMap model){
		List<Student> students=sService.getAllStudent();
		model.put("students", students);
		model.put("page", "student.jsp");
		return "manage/main";
	}
	
	//进入添加管理员的页面
	@RequestMapping("addManager")
	public String addManager(){
		return "manage/addManager";
	}
	
	//保存管理员
	@RequestMapping("saveManager")
	public String saveManager(Manager manager,ModelMap model){
		System.out.println("manager id:"+manager.getId());
		mService.saveManager(manager);

		return "redirect:manager.do";
	}
	
	//Show student results.
	@RequestMapping("stuResult")
	public String stuResult(ModelMap model){
		List<StuResult> results=srService.getAllStuResults();
		
		model.put("results",results);
		model.put("page", "stuResult.jsp");
		return "manage/main";
	}
	
	//Show lessons.
	@RequestMapping("lesson")
	public String lesson(ModelMap model){
		List<Lesson> lessons=lService.getAllLessons();
		
		model.put("lessons", lessons);
		model.put("page", "lesson.jsp");
		return "manage/main";
	}
	
	//Show papers.
	@RequestMapping("paper")
	public String paper(ModelMap model){
		List<Paper> papers=pService.getAllPapers();
		
		model.put("papers", papers);
		model.put("page", "paper.jsp");
		return "manage/main";
	}
	
	//Show questions.
	@RequestMapping("question")
	public String question(ModelMap model){
		List<Question> questions=qService.getAllQuestions();
		
		model.put("questions", questions);
		model.put("page", "question.jsp");
		return "manage/main";
	}
	
	//退出后台
	@RequestMapping("quit")
	public String quit(HttpSession session){
		session.removeAttribute("loginManager");
		return "redirect:goLogin.do";
	}
}
